package com.packagetracker.carriers.handlers;

import java.net.HttpURLConnection;
import java.net.URL;

import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;

import com.google.android.maps.MapActivity;
import com.packagetracker.R;
import com.packagetracker.carriers.PackageDetails;
import com.packagetracker.carriers.PackageLocation;

public class UspsHandler extends PackageHandler {
    
	private static final long serialVersionUID = -5658947433890574451L;
	
	public void parsePackageActivity(final MapActivity activity, final String number) {
	    	super.setMapActivity(activity);
	    	
		try {
			final HttpURLConnection conn = (HttpURLConnection)new URL(this.getValidationData(number).toString().replaceAll(" ", "%20")).openConnection();
			conn.setDoInput(true);
			conn.setDoOutput(true);
			conn.setUseCaches(false);
			conn.setRequestMethod("GET");
			
			SAXParserFactory.newInstance().newSAXParser().parse(new InputSource(conn.getInputStream()), this);
		}
		catch (Exception e) {}
	}
	
	@Override
	public void startElement(final String uri, final String local, final String name, final Attributes atts) {
		if ("TrackSummary".equals(local) || "TrackDetail".equals(local)) {
			this.setPackageDetails(new PackageDetails());
			this.setPackageLocation(new PackageLocation());
		}
		
		this.setTag(local);
	}
	
	@Override
	public void endElement(final String uri, final String local, final String name) {
		if ("TrackSummary".equals(local) || "TrackDetail".equals(local)) {
			if (this.getPackageActivity().locationExists(this.getPackageLocation().getCity(), this.getPackageLocation().getStateProvinceCode())) {
				this.getPackageActivity().getLocation(this.getPackageLocation().getCity(), this.getPackageLocation().getStateProvinceCode()).addDetails(this.getPackageDetails());
			}
			else {
				this.getPackageLocation().addDetails(this.getPackageDetails());
				this.getPackageActivity().addLocation(this.getPackageLocation());
			}
			
			this.setPackageLocation(null);
			this.setPackageDetails(null);
		}
		
		if (this.getPackageLocation() != null) {
			if ("EventCity".equals(local)) {
				this.getPackageLocation().setCity(this.getDataSet().getString());
			}
			else if ("EventState".equals(local)) {
				this.getPackageLocation().setStateProvinceCode(this.getDataSet().getString());
			}
		}
		
		if (this.getPackageDetails() != null) {
			if ("Event".equals(local)) {
				this.getPackageDetails().setDescription(this.getDataSet().getString());
			}
			else if ("EventDate".equals(local)) {
				this.getPackageDetails().setDate(this.getDataSet().getString());
			}
			else if ("EventTime".equals(local)) {
				this.getPackageDetails().setTime(this.getDataSet().getString());
			}
		}
	}
	
	@Override
	public void characters(final char chars[], final int start, final int length) {
		if ("EventCity".equals(this.getTag()) || "EventState".equals(this.getTag()) || "Event".equals(this.getTag()) || "EventDate".equals(this.getTag()) || "EventTime".equals(this.getTag())) {
			this.getDataSet().setString(new String(chars, start, length)); 
		}
	}
	
	private String getValidationData(final String number) {
		final StringBuffer xml = new StringBuffer();
		xml.append(this.getMapActivity().getString(R.string.usps_url));
		xml.append("?API=");
		xml.append(this.getMapActivity().getString(R.string.usps_api));
		xml.append("&XML=<TrackFieldRequest USERID=\"");
		xml.append(this.getMapActivity().getString(R.string.usps_userid));
		xml.append("\" PASSWORD=\"");
		xml.append(this.getMapActivity().getString(R.string.usps_password));
		xml.append("\"><TrackID ID=\"");
		xml.append(number);
		xml.append("\"></TrackID></TrackFieldRequest>");
		
		return xml.toString();
	}
	
}